package merchant.space;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.Spinner;

public class AddCollectionActivity extends Activity
{
	
	AddCollectionActivity context;
	ImageView add_item;
	
	RadioButton priv,publ;
	Spinner spinner;
	String array_spinner[];
	EditText col_name, descrip;

  
	public void onCreate(Bundle savedInstanceState) {
		  super.onCreate(savedInstanceState);
	   //   setContentView(R.layout.addcollection);
		  View viewToLoad = LayoutInflater.from(this.getParent()).inflate(R.layout.addcollection, null);
		  this.setContentView(viewToLoad);  
		  
	      context = this;
	      
	      col_name = (EditText) findViewById(R.id.col_name);
	      descrip = (EditText) findViewById(R.id.description);
	      add_item = (ImageView) findViewById(R.id.addbutton);
	      
	      descrip.setOnFocusChangeListener(new OnFocusChangeListener()
	        {
	   		   public void onFocusChange(View v, boolean hasFocus) {
	   		   // TODO Auto-generated method stub
	   			   if (hasFocus==true)
		   		   {
		   			 descrip.setText("");
		   		     descrip.setInputType(InputType.TYPE_CLASS_TEXT );
		   		   }
	   			   else
	   			   {
		   			 if(descrip.getText().toString().trim().equals(""))
		   			 {
		   				descrip.setInputType(InputType.TYPE_CLASS_TEXT);
		   				 descrip.setText("Description");
		   			 }
	   		   }  }  });
	      
	      col_name.setOnFocusChangeListener(new OnFocusChangeListener()
	        {
	   		   public void onFocusChange(View v, boolean hasFocus) {
	   		   // TODO Auto-generated method stub
	   			   if (hasFocus==true)
		   		   {
	   				col_name.setText("");
	   				col_name.setInputType(InputType.TYPE_CLASS_TEXT );
		   		   }
	   			   else
	   			   {
		   			 if(col_name.getText().toString().trim().equals(""))
		   			 {
		   				col_name.setInputType(InputType.TYPE_CLASS_TEXT);
		   				col_name.setText("Collection Name");
		   			 }
	   		   }  }  });
	      
	      spinner = (Spinner) findViewById(R.id.spinner_category);
	      
	      ArrayAdapter adapter = ArrayAdapter.createFromResource(
	    		 getParent().getParent(), R.array.cattegory_array, android.R.layout.simple_spinner_item);
	      adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	      spinner.setAdapter(adapter);
	    
	      priv = (RadioButton) findViewById(R.id.priv);
	      publ = (RadioButton) findViewById(R.id.publ);
	  
	      priv.setOnClickListener(new OnClickListener() {
	    	  public void onClick(View v){
	    		  if(publ.isChecked()){
	    			  publ.setChecked(false);
	    		  }
	      }});
	      
	      publ.setOnClickListener(new OnClickListener() {
	    	  public void onClick(View v){
	    		  if(priv.isChecked()){
	    			  priv.setChecked(false);
	    		  }
	      }});
	      
	      priv.setChecked(true);
	      
	      add_item.setOnClickListener(new OnClickListener() {
			   public void onClick(View v) { 
				   if(priv.isChecked() | publ.isChecked()){
					   if(col_name.getText().toString() != "Collection Name" && descrip.getText().toString() != "Description"){
						   
						   int id=100;
						   String name = col_name.getText().toString();
						   String des = descrip.getText().toString();
						   int vis;
						   if (priv.isChecked()) vis =0;
						   else vis = 1;
						   MyCollectionsGroupActivity parent = ( MyCollectionsGroupActivity)getParent();
						   String u = parent.getNavigation().getName();
						   String url = "http://merchant-ldso.heroku.com/missing_thumb.png";
						   String cat = spinner.getSelectedItem().toString();
						   
						   
						   Collection newcoll = new Collection(id,name,des,vis,u,url,cat);
						   DataLink.getMyCollections().addCollection(newcoll);
						  // DataLink.myCollection=newcoll;
						   DataLink.setMyColletionc(newcoll);
						   
						   
						   Intent viewcoll = new Intent(getParent(), AddItemActivity.class);
				    	//	 parent.startChildActivity("AddItemActivity", viewcoll);
				    		 
				    		 Log.d("BREAAAK","BREAK");
				    		 context.finish();
				    		 parent.startChildActivity("AddItemActivity", viewcoll);
						  
					   }
				   }
			   }});
	      
	      
	      
	}
}
